﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace HW2
{
    class Solution1
    {   //solution of b1
        public static bool IsPrime(int num)
        {
            for (int i = 2; i < num; i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        public static void solve(int num)
        {
            String info = num.ToString();
            Console.WriteLine(info + "的质数因子为：");
            for (int i = 2; i < num; i++)
            {
                if (num % i == 0 && IsPrime(num / i))
                {
                    Console.WriteLine(i);
                }
            }
        }
    }

    class Solution2
    {
        //solution of b2
        public static void solve()
        {
            ArrayList array = new ArrayList();
            Console.WriteLine("输入数组：");
            bool tag = true;
            while (tag)
            {
                Console.Write("输入(退出输入n)");
                String s = Console.ReadLine();
                if (s == "n")
                {
                    tag = false;
                    continue;
                }
                array.Add(Double.Parse(s));
            }
            double mmax = -3275610;
            double mmin = 3275610;
            double ssum = 0;
            foreach (double i in array) {
                if (i < mmin)
                {
                    mmin = i;
                }
                if (i > mmax)
                {
                    mmax = i;
                }
                ssum += i;
            }
            double avg = ssum / array.Count;
            Console.WriteLine("Max number of this array: " + mmax);
            Console.WriteLine("Min number of this array: " + mmin);
            Console.WriteLine("Sum of this array: " + ssum);
            Console.WriteLine("Average of this array: " + avg);
        }
    }

    class Solution3
    {
        public static void solve()
        {
            bool[] isprime = new bool[100];
            for (int i = 0; i < 100; i++)
            {
                isprime[i] = true;
            }
            isprime[0] = isprime[1] = false;
            for (int i = 2; i < 100; i++)
            {
                int j = 2;
                while (j * i < 100)
                {
                    isprime[j * i] = false;
                    j++;
                }
            }
            Console.WriteLine("100以内的素数为：");
            for (int i = 0; i < 100; i++)
            {
                if (isprime[i])
                {
                    Console.Write(i + " ");
                }
            }
        }
    }


    //solution of b4

    abstract class AbArea
    {
        // 抽象图形类
        public abstract double Area(); // 计算图形面积
        public abstract bool isLegal(); // 判断图形是否合法
    }

    class Square : AbArea
    {
        // 正方形类
        private double length;
        public double Length { get; set; }
        public override double Area()
        {
            return Length * Length;
        }
        public override bool isLegal()
        {
            return Length >= 0;
        }
    }

    class Rectangle : AbArea
    {
        // 矩形类
        private double length;
        private double width;
        public double Length { get; set; }
        public double Width { get; set; }
        public override double Area()
        {
            return Length * Width;
        }
        public override bool isLegal()
        {
            return (Length >= 0 && Width >= 0);
        }
    }

    class Triangle : AbArea
    {
        //三角形类
        private double l1;
        private double l2;
        private double l3;
        public double L1 { get; set; }
        public double L2 { get; set; }
        public double L3 { get; set; }
        public override bool isLegal()
        {
            if (L1 > 0 && L2 > 0 && L3 > 0)
            {
                double m = (L1 > L2) ? L1 : L2;
                double mm = (m > L3) ? m : L3;
                double n = (L1 < L2) ? L1 : L2;
                double nn = (n < L3) ? n : L3;
                double temp = L1 + L2 + L3 - mm - nn;
                return (temp + nn > mm);
            }
            else
            {
                return false;
            }
        }
        public override double Area()
        {
            double p = (L1 + L2 + L3) / 2;
            return Math.Sqrt(p * (p - L1) * (p - L2) * (p - L3));
        }
    }


    // solution of b5
    abstract class Shape
    {
        //抽象形状类
        public string name;
        public double area;
        public abstract void setName();
        public abstract void setArea();
    }

    class Shape1 : Shape
    {
        // 正方形类
        private double edge; 
        public Shape1(double e)
        {
            if(e > 0)
            {
                edge = e;
            }
            else
            {
                edge = 0;
            }
        }
        public override void setName()
        {
            name = "Square";
        }
        public override void setArea()
        {
            area = edge * edge;
        }
    }

    class Shape2 : Shape
    {
        //矩形类
        private double length;
        private double width;
        public Shape2(double l, double w)
        {
            width = (w > 0) ? w : 0;
            length = (l > 0) ? l : 0;
        }
        public override void setName()
        {
            name = "Rectangle";
        }
        public override void setArea()
        {
            area = width * length;
        }
    }

    class Shape3 : Shape
    {
        //三角形类
        private double[] edges = { 0,0,0};
        public Shape3(double[] es)
        {
            if(es[0] > 0 && es[1] > 0 && es[2] > 0) {

                
                double m = (es[0] > es[1]) ? es[0] : es[1];
                double mm = (m > es[2]) ? m : es[2];
                double n = (es[0] < es[1]) ? es[0] : es[1];
                double nn = (m < es[2]) ? m : es[2];
                double temp = es[0] + es[1] + es[2] - mm - nn;
                bool tag = (temp + nn > mm);
                if (tag)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        edges[i] = es[i];
                    }
                }

            }
            else
            {
                for (int i = 0; i < 3; i++)
                {
                    edges[i] = 0;
                }
            }

        }
        public override void setName()
        {
            name = "Triangle";
        }
        public override void setArea()
        {
            double p = (edges[0] + edges[1] + edges[2]) / 2;
            area = Math.Sqrt(p * (p - edges[0]) * (p - edges[1]) * (p - edges[2]));
        }
    }

    class Factory
    {
        // 简单工厂类
        public Shape produce(string pro)
        {
            int productType;
            Shape s = null;
            switch (pro)
            {
                case "Square": productType = 1; break;
                case "Rectangle": productType = 2; break;
                case "Triangle": productType = 3; break;
                default: productType = -1; break;
            }
            switch (productType)
            {
                case 1:
                    Console.WriteLine("***生产中***");
                    Console.WriteLine("请输入正方形边长：");
                    double edge;
                    string temp1 = Console.ReadLine();
                    edge = Double.Parse(temp1);
                    s = new Shape1(edge);
                    Console.WriteLine("正方形生产完毕！");
                    break;
                case 2:
                    Console.WriteLine("***生产中***");
                    Console.WriteLine("请输入长方形边长(以空格分开)：");
                    double e1, e2;
                    string temp2 = Console.ReadLine();
                    string[] es1 = temp2.Split();
                    e1 = Double.Parse(es1[0]);
                    e2 = Double.Parse(es1[1]);
                    s = new Shape2(e1, e2);
                    Console.WriteLine("长方形生产完毕！");
                    break;
                case 3:
                    Console.WriteLine("***生产中***");
                    Console.WriteLine("请输入三角形边长(以空格分开)：");
                    double[] edges = { 0,0,0};
                    string temp3 = Console.ReadLine();
                    string[] es = temp3.Split();
                    edges[0] = Double.Parse(es[0]);
                    edges[1] = Double.Parse(es[1]);
                    edges[2] = Double.Parse(es[2]);
                    s = new Shape3(edges);
                    Console.WriteLine("三角形生产完毕！");
                    break;
                default:
                    Console.WriteLine("无法生产该产品！");
                    break;
            }
            return s;
        }

}

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-----测试AbArea()类及其子类-----");
            Square s = new Square();// 创建正方形对象
            s.Length = 3.12;
            Console.WriteLine(s.Area());
            Console.WriteLine(s.isLegal());
            Rectangle r = new Rectangle();// 创建矩形对象
            r.Length = 5.55;
            r.Width = 4.14;
            Console.WriteLine(r.Area());
            Console.WriteLine(s.isLegal());
            Triangle t = new Triangle();// 创建三角形对象
            t.L1 = 3;
            t.L2 = 4;
            t.L3 = 5;
            Console.WriteLine(t.isLegal());
            Console.WriteLine(t.Area());
            Console.WriteLine("-----测试简单工厂-----");
            Dictionary<int, string> dict = new Dictionary<int, string>();// 产品编号字典
            dict.Add(1, "Square");
            dict.Add(2, "Rectangle");
            dict.Add(3, "Triangle");
            Random rnd = new Random();// 随机生成产品
            double totalSquare = 0; // 总面积
            Factory fac = new Factory(); // 简单工厂对象
            List <Shape> products = new List<Shape>();
            Console.WriteLine("-----工厂开始生产-----");
            for(int i = 0; i < 3; i++)
            {
                products.Add(fac.produce(dict[rnd.Next(1, 4)])); //随机生产产品
            }
            Console.WriteLine("-----工厂生产完毕-----");
            foreach(Shape p in products)
            {
                p.setArea();
                p.setName();
                totalSquare += p.area;
            }
            Console.WriteLine("产品的总面积为：" + totalSquare);
        }
    }
}
